﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using DiamondDash.Core;
using DiamondDash.Utils;
using System.Windows.Threading;
using OpenXLive.Features;
using OpenXLive;

namespace DiamondDash.Pages
{
    public partial class CompletePage : PhoneApplicationPage
    {
        bool isLoaded = false;

        public CompletePage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            //清除页面堆栈
            try
            {
                for (int i = 0; i < 4; i++)
                {
                    NavigationService.RemoveBackEntry();
                }
            }
            catch
            { }

            if (!isLoaded)
            {
                //计算
                int score = ScoreHelper.GetFinalScore(StageManager.Instance.LastestGameScore, StageManager.Instance.CurrLevel);
                int addCoin = ScoreHelper.GetCoin(score);
                int addExp = ScoreHelper.GetExp(score);
                int oldLevelBonus = ScoreHelper.GetLevelBonus(StageManager.Instance.CurrLevel);

                bool isLevelUP = StageManager.Instance.AddExp(addExp);
                StageManager.Instance.TotalG += addCoin;
                if (score > StageManager.Instance.HighScore)
                {
                    StageManager.Instance.HighScore = score;
                    SoundManager.PlayEffect(SoundManager.highScore);
                    this.StoryboardNewHigh.Begin();

                    //上传分数到排行榜
                    Leaderboard lb = new Leaderboard(XLiveGameManager.CurrentSession, "f89220dd-4440-42ab-a2a4-ba08b4c14f5c");
                    lb.SubmitScoreCompleted += lb_SubmitScoreCompleted;
                    lb.SubmitScore(score);
                }

                //展示
                this.levelProgress.SetLevelProgress(StageManager.Instance.CurrLevel
                    , StageManager.Instance.GetLevelExpPercent());
                this.txtCoinCount.Text = StageManager.Instance.TotalG.ToString();
                this.txtScore.Text = score.ToString();
                this.txtBonusPercent.Text = "+ " + oldLevelBonus + "%";
                this.txtAddCoin.Text = addCoin.ToString();
                this.txtAddExp.Text = addExp.ToString();

                if (isLevelUP)
                {
                    this.txtNewBonusPercent.Text = "+ " + ScoreHelper.GetLevelBonus(StageManager.Instance.CurrLevel) + "%";
                    this.txtNewLevel.Text = StageManager.Instance.CurrLevel.ToString();
                    this.btnOK.IsEnabled = false;

                    DispatcherTimer tempTimer = new DispatcherTimer();
                    tempTimer.Interval = TimeSpan.FromSeconds(3);
                    tempTimer.Tick += (object ss, EventArgs ee) =>
                    {
                        tempTimer.Stop();
                        this.StoryboardShowLevelUp.Completed += StoryboardShowLevelUp_Completed;
                        this.StoryboardShowLevelUp.Begin();
                    };
                    tempTimer.Start();

                }

                isLoaded = true;
            }
        }

        void StoryboardShowLevelUp_Completed(object sender, EventArgs e)
        {
            this.btnOK.IsEnabled = true;
            SoundManager.PlayEffect(SoundManager.levelup);
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void btnOK_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.clickButton);
            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }

        void lb_SubmitScoreCompleted(object sender, AsyncEventArgs e)
        {

        }
    }
}